package reflecct;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @classname:MethodDemo
 * @description:
 * @author: Mr.Z
 * @date: 2020/12/23 20:37
 * @version: 0.1
 * @since:1.8
 */
public class MethodDemo {
    public static void main(String[] args) {
        methodDemo();
    }

    private static void methodDemo() {
        Map<String, Object> map = new HashMap<>(16);
        map.put("name", "jim");
        map.put("pass", "1234");
        map.put("phone", "199201224445");
        try {
            Class<?> aClass = Class.forName("reflecct.User");
            User user = (User) aClass.newInstance();
            map.forEach((key, value) -> {
                StringBuilder builder = new StringBuilder(key);
                builder.setCharAt(0, Character.toUpperCase(key.charAt(0)));
                String methodName = "set" + builder.toString();
                try {
                    //value.getClass得到的是什么
                    Method method = aClass.getMethod(methodName, value.getClass());
                    method.invoke(user, value);
                } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                    e.printStackTrace();
                }
            });
            System.out.println(user);
        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
            e.printStackTrace();
        }
    }
}
